package com.mark.common.util.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @category 提供一些访问数据基础操作接口
 * @author Mark Liang
 *
 */
public class DaoBase {
	
	private final Log logger = LogFactory.getLog(DaoBase.class);
	protected DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	
	
	public int[] batch(String sql, Object[][] params) throws SQLException {
		QueryRunner queryRunner = getInstance();
		int[] affectedRows = new int[0];
		try {
			affectedRows = queryRunner.batch(sql, params);
		} catch (SQLException e) {
			logger.error("Error occured while attempting to batch update data",
					e);
			throw e;
		}
		return affectedRows;
	}
	
	/**
	 * 执行批量sql语句
	 * 
	 * @param sql
	 *            sql语句
	 * @param params
	 *            参数list
	 * @return 受影响的行数的数组
	 * @throws SQLException
	 */
	public int[] batch(String sql, Collection<Object[]> params) throws SQLException {
		BaseQueryRunner queryRunner = getInstance();
		int[] affectedRows = new int[0];
		try {
			affectedRows = queryRunner.batch(sql, params);
		} catch (SQLException e) {
			logger.error("Error occured while attempting to batch update data",
					e);
			throw e;
		}
		return affectedRows;
	}
	
	/**
	 * 查询所有某一列的值
	 * 
	 * @param sql
	 *            sql语句
	 * @param params
	 *            参数数组
	 * @return 查询结果
	 * @throws SQLException
	 */
	public <T> List<T> findColumes(String sql,Object... params) throws SQLException {
		QueryRunner queryRunner = getInstance();
		List<T> columns = new ArrayList<T>();
		ResultSetHandler<List<T>> handler = new ResultSetHandler<List<T>>() {
			//@Override
			public List<T> handle(ResultSet rs) throws SQLException {
				List<T> lists = new ArrayList<T>();
				while (rs.next()) {
					@SuppressWarnings("unchecked")
					T t = (T) rs.getObject(1);
					lists.add(t);
				}
				return lists;
			}

		};
		try {
			if (params == null) {
				columns = queryRunner.query(sql, handler);
			} else {
				columns = queryRunner.query(sql, handler, params);
			}
			return columns;
		} catch (SQLException e) {
			logger.error("Error occured while attempting to findColume ", e);
			throw e;
		}
	}
	
	/**
	 * 执行查询，将每行的结果保存到Bean中，然后将所有Bean保存到List中
	 * 
	 * @param <T>
	 * @param sql
	 * @param handler
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public <T> List<T> findBeans(String sql, ResultSetHandler<List<T>> handler,
			Object... params) throws SQLException {
		QueryRunner queryRunner = getInstance();
		List<T> list = new ArrayList<T>();
		try {
			if (params == null) {
				list = queryRunner.query(sql, handler);
			} else {
				list = queryRunner.query(sql, handler, params);
			}
		} catch (SQLException e) {
			logger.error("Error occured while attempting to query data list", e);
			e.printStackTrace();
			throw e;
		}
		return list;
	}
	
	/**
	 * 根据条件获取bean
	 * 
	 * @param <T>
	 * @param sql
	 * @param handler
	 * @param params
	 * @return
	 * @throws SQLException
	 */
	public <T> T findBean(String sql, ResultSetHandler<T> handler,
			Object... params) throws SQLException {
		QueryRunner queryRunner = getInstance();
		T t;
		try {
			t = queryRunner.query(sql, handler, params);
		} catch (SQLException e) {
			logger.error("Error occured while attempting to findBeab ", e);
			throw e;
		}

		return t;
	}
	
	

	
	/**
	 * 获取queryrunner
	 * 
	 * @return
	 */
	public BaseQueryRunner getInstance() {
		return new BaseQueryRunner(dataSource);
	}
	

}
